﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.JsonObjectAttribute

Public Class Youdao : Implements InterfaceTransfer
    Private Const URL As String = "http://fanyi.youdao.com/translate"
    Public Function trans(q As String, from_code As String, to_code As String) As String Implements InterfaceTransfer.trans
        Try
            Dim helper As New HttpHelp
            Dim par As String
            par = "doctype=json&type=" & from_code & "2" & to_code & "&i=" & System.Web.HttpUtility.UrlEncode(q, Text.Encoding.UTF8)
            Dim result As String = Trim(helper.AllGETHttpRequest(URL, par))
            Dim ret As Object = CType(JsonConvert.DeserializeObject(result), Object)
            Dim r As String = ""

            If ret("translateResult") IsNot Nothing Then
                Dim temp = ret("translateResult")
                For Each c In temp
                    For Each d In c
                        r &= d("tgt")
                    Next
                Next
            End If
            Return r
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function
End Class
